﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mouse_Controller : MonoBehaviour {
    public float JetPower = 75.0f;//火箭喷力
    private Rigidbody2D rig;//刚体组件
    private bool isJetActive = false;//是否点击鼠标
    public float FlySpeed = 3.0f;

    public LayerMask Ground;//地板层
    private Transform GroundCheckPoint;//脚本检测点

    public ParticleSystem JetParticle;
	// Use this for initialization
	void Start () {
        rig = GetComponent<Rigidbody2D>();
        GroundCheckPoint = transform.Find("GroundCheck");
	}
	
	// Update is called once per frame
	void Update () {
        isJetActive = Input.GetMouseButton(0);
        //判断老鼠是否着地
        bool isGrounded = Physics2D.OverlapCircle(
            GroundCheckPoint.transform.position,
            0.1f, Ground);
        //粒子系统的处理
        ParticleSystem.EmissionModule em = JetParticle.emission;
        em.enabled = !isGrounded;
        em.rateOverTime = isJetActive ? 300.0f : 75.0f;

        //if (Physics2D.OverlapCircle(
        //    GroundCheckPoint.transform.position, 
        //    0.1f, Ground))
        //{
        //    Debug.Log("地上");
        //    //wrong code:
        //    //JetParticle.emission.enabled = false;
        //    //right code:
        //    ParticleSystem.EmissionModule em = JetParticle.emission;
        //    em.enabled = false;

        //}
        //else
        //{
        //    Debug.Log("天上");
        //    ParticleSystem.EmissionModule em = JetParticle.emission;
        //    em.enabled = true;
        //}
        
	}

    private void FixedUpdate()
    {
        //Debug.Log(isJetActive);

        if (rig != null)
        {
            if (isJetActive)
            {
                rig.AddForce(JetPower * Vector2.up);
            }
            rig.velocity = new Vector2(FlySpeed, rig.velocity.y);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        //Debug.Log(collision);
        if (collision.CompareTag("MyCoin"))
        {
            Debug.Log("和金币碰上了");
            //只是隐藏
            //collision.gameObject.SetActive(false);
            //销毁金币
            Destroy(collision.gameObject);
        }
        if (collision.CompareTag("Harm"))
        {
            Debug.Log("老鼠被干掉了");
        }
    }
}
